import request from '../util/axios'

// 设备管理API服务
export const deviceApi = {
  // 获取设备列表（分页查询）
  getDeviceList(params) {
    return request({
      url: '/devices',
      method: 'get',
      params: {
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10,
        deviceType: params.deviceType,
        status: params.status,
        keyword: params.keyword
      }
    })
  },

  // 获取设备详情
  getDeviceDetail(id) {
    return request({
      url: `/devices/${id}`,
      method: 'get'
    })
  },

  // 新增设备
  addDevice(data) {
    return request({
      url: '/devices',
      method: 'post',
      data
    })
  },

  // 更新设备
  updateDevice(data) {
    return request({
      url: '/devices',
      method: 'put',
      data
    })
  },

  // 删除设备
  deleteDevice(id) {
    return request({
      url: `/devices/${id}`,
      method: 'delete'
    })
  },

  // 获取设备状态统计
  getDeviceStats() {
    return request({
      url: '/devices/stats',
      method: 'get'
    })
  }
}

// 设备状态相关API
export const deviceStatusApi = {
  // 检查设备在线状态
  checkDeviceOnline(id, timeoutMillis = 30 * 60 * 1000) {
    return request({
      url: `/devices/${id}/online`,
      method: 'get',
      params: { timeoutMillis }
    })
  },

  // 获取设备状态日志
  getDeviceStatusLogs(id, params) {
    return request({
      url: `/devices/${id}/status-logs`,
      method: 'get',
      params: {
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10
      }
    })
  },

  // 设备心跳上报
  reportHeartbeat(id, ttlMinutes = 45) {
    return request({
      url: `/devices/${id}/heartbeat`,
      method: 'post',
      params: { ttlMinutes }
    })
  }
}

// 设备配置相关API
export const deviceConfigApi = {
  // 获取设备配置
  getDeviceConfig(id) {
    return request({
      url: `/devices/${id}/configs`,
      method: 'get'
    })
  },

  // 设置设备配置
  setDeviceConfig(id, config) {
    return request({
      url: `/devices/${id}/configs`,
      method: 'put',
      data: config
    })
  },

  // 配置ACK回执
  configAck(id, version) {
    return request({
      url: `/devices/${id}/config-ack`,
      method: 'post',
      params: { version }
    })
  }
}

// 设备分组相关API
export const deviceGroupApi = {
  // 获取分组树
  getGroupTree() {
    return request({
      url: '/device-groups/tree',
      method: 'get'
    })
  },

  // 新增分组
  addGroup(data) {
    return request({
      url: '/device-groups',
      method: 'post',
      data
    })
  },

  // 删除分组
  deleteGroup(id) {
    return request({
      url: `/device-groups/${id}`,
      method: 'delete'
    })
  },

  // 绑定设备到分组
  bindDevicesToGroup(id, deviceIds) {
    return request({
      url: `/device-groups/${id}/devices`,
      method: 'put',
      data: deviceIds
    })
  }
}

// 设备维护相关API
export const deviceMaintenanceApi = {
  // 获取维护计划列表
  getMaintenancePlans(params) {
    return request({
      url: '/maintenance/plans',
      method: 'get',
      params: {
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10,
        deviceId: params.deviceId,
        status: params.status
      }
    })
  },

  // 新增维护计划
  addMaintenancePlan(data) {
    return request({
      url: '/maintenance/plans',
      method: 'post',
      data
    })
  },

  // 删除维护计划
  deleteMaintenancePlan(id) {
    return request({
      url: `/maintenance/plans/${id}`,
      method: 'delete'
    })
  },

  // 更新维护计划状态
  updateMaintenancePlanStatus(id, status) {
    return request({
      url: `/maintenance/plans/${id}/status`,
      method: 'patch',
      params: { status }
    })
  },

  // 获取维护记录列表
  getMaintenanceRecords(params) {
    return request({
      url: '/maintenance/records',
      method: 'get',
      params: {
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10,
        deviceId: params.deviceId,
        planId: params.planId
      }
    })
  },

  // 新增维护记录
  addMaintenanceRecord(data) {
    return request({
      url: '/maintenance/records',
      method: 'post',
      data
    })
  }
}

export default {
  deviceApi,
  deviceStatusApi,
  deviceConfigApi,
  deviceGroupApi,
  deviceMaintenanceApi
}
